package gizmoball;

import java.awt.Graphics;

public class TraBrick extends Brick
{
	int width = 40;
	int height = 20;
	TriBrick t1;
	TriBrick t2;
	RecBrick r1;
	
	public TraBrick(int x,int y)
	{
		super(x,y);
		r1 = new RecBrick(x-width/2,y-height/2,this.property,0);
		t1 = new TriBrick(x-width/2-height/2,y,this.property,0);
		t2 = new TriBrick(x+width/2+height/2,y,this.property,90);
	}
	
	public TraBrick(int x,int y,int p,int d)
	{
		super(x,y,p,d);
		r1 = new RecBrick(x-width/2,y-height/2,this.property,0);
		t1 = new TriBrick(x-width/2-height/2,y,this.property,0);
		t2 = new TriBrick(x+width/2+height/2,y,this.property,90);
		if(d == 90)
		{
			this.direction = 0;
			this.rotation();
		}
		else if(d == 180)
		{
			this.direction = 0;
			this.rotation();
			this.rotation();
		}
		else if(d == 270)
		{
			this.direction = 0;
			this.rotation();
			this.rotation();
			this.rotation();
		}
		
	}
	
	public TraBrick(TraBrick b)
	{
		super(b);
		this.width = b.width;
		this.height = b.height;
		this.r1 = new RecBrick(b.r1);
		this.t1 = new TriBrick(b.t1);
		this.t2 = new TriBrick(b.t2);
	}
	
	public void setProperty(int property) {
		this.property = property;
		r1.setProperty(property);
		t1.setProperty(property);
		t2.setProperty(property);
	}
	
	public void setBrickx(int brickx) 
	{
		this.brickx = brickx;
		if(this.direction == 0)
		{
			r1.setBrickx(this.brickx-this.width/2);
			t1.setBrickx(this.brickx-this.width/2-this.height/2);
			t2.setBrickx(this.brickx+this.width/2+this.height/2);
		}
		else if(this.direction == 90)
		{
			r1.setBrickx(this.brickx-this.height/2);
			t1.setBrickx(this.brickx);
			t2.setBrickx(this.brickx);
		}
		else if(this.direction == 180)
		{
			r1.setBrickx(this.brickx-this.width/2);
			t1.setBrickx(this.brickx+this.width/2+this.height/2);
			t2.setBrickx(this.brickx-this.width/2-this.height/2);
		}
		else if(this.direction == 270)
		{
			r1.setBrickx(this.brickx-this.height/2);
			t1.setBrickx(this.brickx);
			t2.setBrickx(this.brickx);
		}
	}
	
	public void setBricky(int bricky) 
	{
		this.bricky = bricky;
		if(this.direction == 0)
		{
			r1.setBricky(this.bricky-this.height/2);
			t1.setBricky(this.bricky);
			t2.setBricky(this.bricky);
		}
		else if(this.direction == 90)
		{
			r1.setBricky(this.bricky-this.width/2);
			t1.setBricky(this.bricky-this.width/2-this.height/2);
			t2.setBricky(this.bricky+this.width/2+this.height/2);
		}
		else if(this.direction == 180)
		{
			r1.setBricky(this.bricky-this.height/2);
			t1.setBricky(this.bricky);
			t2.setBricky(this.bricky);
		}
		else
		{
			r1.setBricky(this.bricky-this.width/2);
			t1.setBricky(this.bricky+this.width/2+this.height/2);
			t2.setBricky(this.bricky-this.width/2-this.height/2);
		}
	}
	
	public void paint(Graphics g)
	{
		r1.paint(g);
		t1.paint(g);
		t2.paint(g);
	}
	
	public void rotation()
	{
		direction += 90;
		if(direction == 90)
		{
			r1.brickx = this.brickx-this.height/2;
			r1.bricky = this.bricky-this.width/2;
			r1.rotation();
			t1.brickx = this.brickx;
			t1.bricky = this.bricky-this.width/2-this.height/2;
			t2.brickx = this.brickx;
			t2.bricky = this.bricky+this.width/2+this.height/2;
			t1.rotation();
			t2.rotation();
		}
		else if(direction == 180)
		{
			r1.brickx = this.brickx-this.width/2;
			r1.bricky = this.bricky-this.height/2;
			r1.rotation();
			t1.brickx = this.brickx+this.width/2+this.height/2;
			t1.bricky = this.bricky;
			t2.brickx = this.brickx-this.width/2-this.height/2;
			t2.bricky = this.bricky;
			t1.rotation();
			t2.rotation();			
		}
		else if(direction == 270)
		{
			r1.brickx = this.brickx-this.height/2;
			r1.bricky = this.bricky-this.width/2;
			r1.rotation();
			t1.brickx = this.brickx;
			t1.bricky = this.bricky+this.width/2+this.height/2;
			t2.brickx = this.brickx;
			t2.bricky = this.bricky-this.width/2-this.height/2;
			t1.rotation();
			t2.rotation();
		}
		else if(direction == 360)
		{
			direction = 0;
			r1.brickx = this.brickx-this.width/2;
			r1.bricky = this.bricky-this.height/2;
			r1.rotation();
			t1.brickx = this.brickx-this.width/2-this.height/2;
			t1.bricky = this.bricky;
			t2.brickx = this.brickx+this.width/2+this.height/2;
			t2.bricky = this.bricky;
			t1.rotation();
			t2.rotation();
		}
	}
	
	public void large()
	{
		if(this.width<=80)
		{
			if(this.direction == 0)
			{
				t1.large();
				t2.large();
				r1.large();
				this.width = this.width*2;
				this.height = this.height*2;
				r1.brickx = this.brickx-this.width/2;
				r1.bricky = this.bricky-this.height/2;
				t1.brickx = this.brickx-this.width/2-this.height/2;
				t1.bricky = this.bricky;
				t2.brickx = this.brickx+this.width/2+this.height/2;
				t2.bricky = this.bricky;
			}
			else if(this.direction == 90)
			{
				t1.large();
				t2.large();
				r1.large();
				this.width = this.width*2;
				this.height = this.height*2;
				r1.brickx = this.brickx-this.height/2;
				r1.bricky = this.bricky-this.width/2;
				t1.brickx = this.brickx;
				t1.bricky = this.bricky-this.width/2-this.height/2;
				t2.brickx = this.brickx;
				t2.bricky = this.bricky+this.width/2+this.height/2;
			}
			else if(this.direction == 180)
			{
				t1.large();
				t2.large();
				r1.large();
				this.width = this.width*2;
				this.height = this.height*2;
				r1.brickx = this.brickx-this.width/2;
				r1.bricky = this.bricky-this.height/2;
				t1.brickx = this.brickx+this.width/2+this.height/2;
				t1.bricky = this.bricky;
				t2.brickx = this.brickx-this.width/2-this.height/2;
				t2.bricky = this.bricky;
			}
			else
			{
				t1.large();
				t2.large();
				r1.large();
				this.width = this.width*2;
				this.height = this.height*2;
				r1.brickx = this.brickx-this.height/2;
				r1.bricky = this.bricky-this.width/2;
				t1.brickx = this.brickx;
				t1.bricky = this.bricky+this.width/2+this.height/2;
				t2.brickx = this.brickx;
				t2.bricky = this.bricky-this.width/2-this.height/2;
			}
		}
	}
	
	public void small()
	{
		if(width>=40)
		{
			if(this.direction == 0)
			{
				t1.small();
				t2.small();
				r1.small();
				this.width = this.width/2;
				this.height = this.height/2;
				r1.brickx = this.brickx-this.width/2;
				r1.bricky = this.bricky-this.height/2;
				t1.brickx = this.brickx-this.width/2-this.height/2;
				t1.bricky = this.bricky;
				t2.brickx = this.brickx+this.width/2+this.height/2;
				t2.bricky = this.bricky;
			}
			else if(this.direction == 90)
			{
				t1.small();
				t2.small();
				r1.small();
				this.width = this.width/2;
				this.height = this.height/2;
				r1.brickx = this.brickx-this.height/2;
				r1.bricky = this.bricky-this.width/2;
				t1.brickx = this.brickx;
				t1.bricky = this.bricky-this.width/2-this.height/2;
				t2.brickx = this.brickx;
				t2.bricky = this.bricky+this.width/2+this.height/2;
			}
			else if(this.direction == 180)
			{
				t1.small();
				t2.small();
				r1.small();
				this.width = this.width/2;
				this.height = this.height/2;
				r1.brickx = this.brickx-this.width/2;
				r1.bricky = this.bricky-this.height/2;
				t1.brickx = this.brickx+this.width/2+this.height/2;
				t1.bricky = this.bricky;
				t2.brickx = this.brickx-this.width/2-this.height/2;
				t2.bricky = this.bricky;
			}
			else
			{
				t1.small();
				t2.small();
				r1.small();
				this.width = this.width/2;
				this.height = this.height/2;
				r1.brickx = this.brickx-this.height/2;
				r1.bricky = this.bricky-this.width/2;
				t1.brickx = this.brickx;
				t1.bricky = this.bricky+this.width/2+this.height/2;
				t2.brickx = this.brickx;
				t2.bricky = this.bricky-this.width/2-this.height/2;
			}
		}
	}
	
	public int judge(int x,int y,int radius)
	{
		if(r1.judge(x,y,radius)==-1 && t1.judge(x,y,radius)==-1 && t1.judge(x,y,radius)==-1)
			return -1;
		else if(r1.judge(x,y,radius)!=-1 && t1.judge(x,y,radius)==-1 && t2.judge(x,y,radius)==-1)
		{
			this.bump = 0;
			return r1.judge(x,y,radius);
		}
		else if(r1.judge(x,y,radius)==-1 && t1.judge(x,y,radius)!=-1 && t2.judge(x,y,radius)==-1)
		{
			this.bump = 0;
			return t1.judge(x,y,radius);
		}
		else if(r1.judge(x,y,radius)==-1 && t1.judge(x,y,radius)==-1 && t2.judge(x,y,radius)!=-1)
		{
			this.bump = 0;
			return t2.judge(x,y,radius);
		}
		
		else
			return r1.judge(x, y, radius);
	}
	public int judge(int x,int y){
		if(r1.judge(x, y) != -1)
			return -3;
		else if(t1.judge(x, y) != -1)
			return -3;
		else if(t2.judge(x, y) != -1)
			return -3;
		else
			return -1;
	}
}
